package com.dev.ac.web.server;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deepoove.poi.XWPFTemplate;
import com.dev.ac.annotation.Login;
import com.dev.ac.common.Const;
import com.dev.ac.common.Result;
import com.dev.ac.entity.AActive;
import com.dev.ac.entity.AApplication;
import com.dev.ac.entity.AUser;
import com.dev.ac.service.AActiveService;
import com.dev.ac.service.AApplicationService;
import com.dev.ac.service.AUserService;
import com.dev.ac.vo.ActiveVo;
import com.dev.ac.vo.ExamineVo;
import com.dev.ac.vo.SearchVo;
import lombok.extern.java.Log;
import org.apache.xmlgraphics.util.ClasspathResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.stylesheets.LinkStyle;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 付
 * @Date 2024-04-14 17:23
 * @Version 1.0
 * @description
 * @since 1.0
 */
@RestController
@RequestMapping("/admin/active")
public class ActiveController {

	@Autowired
	AActiveService aActiveService;
	@Autowired
	AApplicationService aApplicationService;
	@Autowired
	AUserService aUserService;

	/**
	 * 活动列表
	 *
	 * @param currentPage 当前页
	 * @return
	 */
	@GetMapping("/list")
	public Result list(Integer currentPage) {

		Page<AActive> list = aActiveService.selctAll(currentPage);

		return Result.success(list);
	}

	/**
	 * 活动添加
	 *
	 * @param activeVo 表单数据
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/insert")
	@Login(issuer = Const.GRADE_ADMIN)
	public Result insertActive(@Validated @RequestBody ActiveVo activeVo) throws IOException {
		System.out.println(activeVo);
		Object obj = aActiveService.insertActive(activeVo);
		return Result.success();
	}

	/**
	 * 活动审核
	 *
	 * @param ids
	 * @param type
	 * @return
	 */
	@PostMapping("/examine")
//	@Login(issuer = Const.GRADE_ADMIN)
	public Result examineActive(@RequestBody ExamineVo examineVo) {
		boolean b = aActiveService.examine(examineVo.getIds(), examineVo.getType());
		if (!b) {
			return Result.error("抱歉，出错了");
		}
		return Result.success();
	}

	@GetMapping("/one")
	public Result getOne(Integer id) {
		return Result.success(aActiveService.selectById(id));
	}

	/**
	 * 获取活动审核列表
	 *
	 * @return
	 */
	@Login(issuer = Const.GRADE_ADMIN)
	@GetMapping("/examine-list")
	public Result getExamineList(Integer currentPage) {
		Page<AActive> page = aActiveService.page(new Page<>(currentPage, 8), new QueryWrapper<AActive>()
				.eq("status", 0)
				.ge("dead_time", LocalDateTime.now())
				.orderByDesc("create_time")
		);
		return Result.success(page);
	}

	@GetMapping("/application")
	public Result getApplicationList(Integer activeId) {
		List<AApplication> list = aApplicationService.list(new QueryWrapper<AApplication>()
				.eq("active_id", activeId)
				.orderByDesc("create_time")
		);
		for (AApplication aApplication : list) {
			AUser aUser = aUserService.getById(aApplication.getUserId());
			aApplication.setAUser(aUser);
		}

		return Result.success(list);
	}

	@GetMapping("/delete")
	public Result delete(Integer id) {
		boolean b = aActiveService.deleteById(id);
		return Result.success();
	}

	@PostMapping("/search")
	public Result search(@RequestBody SearchVo searchVo) {
		System.out.println(searchVo);
		Object obj = aActiveService.search(searchVo);
		return Result.success(obj);
	}

}
